package org.filecollector.filecontroller.virtualcatalogue.groupname;

import java.util.Optional;
import java.util.regex.Matcher;

/**
 * Abstract class that produces GroupNames
 */
public abstract class GroupNameFactory {
    /**
     * Parse String and return Group Name
     * @param name string in format defined in {@link GroupNameClass}
     * @return <li>{@code Optional.empty()} if name is not matching</li>
     * <li>{@code Optional<GroupName>} if everything is ok</li>
     */
    public static Optional<GroupName> getGroupNameByString(String name) {
        if (!GroupNameClass.matchesName(name)) {
            return Optional.empty();
        }
        Matcher matcher = GroupNameClass.getNamePattern().matcher(name);
        if (!matcher.find()) {
            return Optional.empty();
        }
        if (matcher.groupCount() != 2) {
            return Optional.empty();
        }
        Long id = Long.parseLong(matcher.group(1), 16);
        byte mask = Byte.parseByte(matcher.group(2));
        return Optional.of(new GroupNameClass(id, mask));
    }

    public static boolean isCorrect(String name) {
        return GroupNameClass.matchesName(name);
    }
}
